package com.oocl.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.oocl.dao.OrdersDao;
import com.oocl.dao.base.BaseJPADaoUtil;
import com.oocl.entity.Orders;


@Repository("ordersDao")
public class OrdersDaoImpl extends BaseJPADaoUtil implements OrdersDao{

	
	/*
     * @Method  findOrderByOid
     * @function load Orders by   oid
     * @param  oid  Orders oid
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrderByOid(String oid) {
		String sql = "select o from Orders o  where o.oid=:oid ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("oid", oid)
		                       .getResultList();
		return list;
	}

	/*
     * @Method  findOrdersByOstate
     * @function load Orders by  ostate
     * @param  ostate  Orders ostate
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrdersByOstate(String ostate) {
		String sql = "select o from Orders o  where o.ostate=:ostate ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("ostate", ostate)
		                       .getResultList();
		return list;
	}
  
	/*
     * @Method  findOrdersByCid
     * @function load Orders by cid
     * @param  cid  Clients cid
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrdersByCid(String cid) {
		String sql = "select o from Orders o left join fetch o.client oc  where oc.cid=:cid ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("cid", cid)
		                       .getResultList();
		return list;
	}
     
	/*
     * @Method  findOrdersByIcnum
     * @function load Orders by   icnum
     * @param  icnum  Merchants  icnum
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrdersByIcnum(String icnum) {
		String sql = "select o from Orders o left join fetch o.merchant om  where om.icnum=:icnum ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("icnum", icnum)
		                       .getResultList();
		return list;
	}
   
	
	@SuppressWarnings("unchecked")
	@Override
	public List<String> finddistinctOid(String cid) {
		String sql = "select distinct o.oid from Orders o where o.client.cid = :cid";
		List<String> list =  entityManager.createQuery(sql).setParameter("cid", cid).getResultList();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> finddistinctOidWithState(String state,String cid) {
		String sql = "select distinct o.oid from Orders o where o.ostate = :state and o.client.cid = :cid";
		return entityManager.createQuery(sql).setParameter("state", state).setParameter("cid", cid).getResultList();
	}
}
